package com.jiayang.hhotel.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.adapter.ContractTimeListAdapter;
import com.jiayang.hhotel.bean.ContractTimeModel;
import com.jiayang.hhotel.presenter.ContractTimePresenter;
import com.jiayang.hhotel.util.ToastUtil;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XActivity;

/**
 * Created by Administrator on 2017/5/26 0026.
 * 合同时间页面（协议客户合同页面）
 */

public class ContractTimeActivity extends XActivity<ContractTimePresenter> implements View.OnClickListener {

    private SwipeMenuRecyclerView recyclerView;
    private SwipeRefreshLayout fresh;
    private Button add;

    private LinearLayoutManager mLinearLayoutManager;

    private int lastVisibleItem;

    private ContractTimeListAdapter adapter;

    private int page;

    @Override
    public void initData(Bundle savedInstanceState) {
        initTitle(this,true,getString(R.string.contract_time),R.color.orange_color,null,null);

        fresh= (SwipeRefreshLayout) findViewById(R.id.fresh);
        recyclerView= (SwipeMenuRecyclerView) findViewById(R.id.recyclerView);
        mLinearLayoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(mLinearLayoutManager);
        add= (Button) findViewById(R.id.bt_bt);
        add.setText(getString(R.string.newly_added));
        add.setOnClickListener(this);

        isFresh(true);
        getP().asyncLoad();
        fresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page=1;
            }
        });

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                if(!recyclerView.canScrollVertically(1)){
//                    LogUtil.showLog("isScroll");
                        getP().asyncLoad();
//                }
                lastVisibleItem = mLinearLayoutManager.findLastVisibleItemPosition();
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if(adapter!=null){
                    if(newState==RecyclerView.SCROLL_STATE_IDLE&&lastVisibleItem+1==adapter.getItemCount()){
                        //todo 请求数据
                        page++;
                        ToastUtil.Toast("开始加载更多",ContractTimeActivity.this);
                    }
                }
            }
        });
    }

    public void setAdapter(List<ContractTimeModel.PTaContractDateListDtoBean> models){
        isFresh(false);
        if(adapter==null){
            adapter=new ContractTimeListAdapter(this,models,null,ContractTimeListAdapter.FLAG_CONTRACT_TIME);
            recyclerView.setAdapter(adapter);
        }else{
            adapter.notifyDataSetChanged();
        }
    }

    private void isFresh(final boolean is){
        fresh.post(new Runnable() {
            @Override
            public void run() {
                fresh.setRefreshing(is);
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_contract;
    }

    @Override
    public ContractTimePresenter newP() {
        return new ContractTimePresenter();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_bt://新增
                Intent intent=new Intent(this,ContractTimeAddedActivity.class);
                startActivity(intent);
                break;
        }
    }
}
